﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using PPAP.BO;
using iTextSharp.text.pdf;
using System.IO;
using PPAP.Model;
using System.Configuration;

namespace PPAP.Web.Part
{
    public partial class PPARAttachmentTab : PageBase
    {
        public string RID { get; set; }
        public string FJRID { get; set; }
        public string QEFJID { get; set; }
        AttachmentTypeBO typebo = new AttachmentTypeBO();
        PPARBO ppbo = new PPARBO();
        LogBO lbo = new LogBO();
        InapplicableBO ibo = new InapplicableBO();
        T_FileBO manager = new T_FileBO();
        MailTemplateBO mbo = new MailTemplateBO();
        string FJID;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                    DataSet ds = typebo.GetList("");
                    ddlType.Items.Clear();
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        ddlType.Items.Add(new ListItem(dr["id"].ToString()+"-"+dr["ATTACHMENTTYPE"].ToString(), dr["id"].ToString()));
                    }
                    
                   PPAR pp = ppbo.GetModel(int.Parse(Request.QueryString["RID"] == null ? "0" : Request.QueryString["RID"].ToString()));
                   if (pp != null)
                   {
                       DropDownList1.SelectedValue = pp.SUBMISSIONLEVEL.ToString();
                       rbtNo.Checked = pp.ISSPECIFICATIONS == 0 ? true : false;
                       rbtYes.Checked = pp.ISSPECIFICATIONS == 1 ? true : false;
                   }
                    LoadType(ds);
                   
            }
            PPAR p = ppbo.GetModel(int.Parse(Request.QueryString["RID"] == null ? "0" : Request.QueryString["RID"].ToString()));
            if (p != null)
            {
               // DropDownList1.SelectedValue = p.SUBMISSIONLEVEL.ToString();
                lblApp.Text = "<input type='button' width='106px'  class='button1' onclick='javascript:PopUpWindow(\"InapplicableSet.aspx?RID=" + p.ID + "\", 230, 60, 730, 550);'   value='Inapplicable'/>";
                if (p.STATUS == 0 || p.STATUS == 1 || p.STATUS == 4)
                {
                    ta1.Visible = false;
                    GridView2.Visible = false;
                    lblApp.Visible = false;

                }
                
                FJRID = p.FJID;
                QEFJID = p.QEFJID;
                ShowData(p.FJID);
                ShowDataQE(QEFJID);
            }
            if (Request.QueryString["deid"] != null)
            {
                DeleteFile(Request.QueryString["deid"].ToString());
            }
            if(Request.QueryString["RID"]!=null)
                RID = Request.QueryString["RID"].ToString();


            if (Request.QueryString["his"] != null && Request.QueryString["his"].ToString() == "1") //从histroy页面跳转过来
            {
                ddlType.Enabled = false;
                rbtNo.Enabled = false;
                rbtYes.Enabled = false;
                btnSubmit.Visible = false;
                lblApp.Visible = false;
                DropDownList1.Enabled = false;
                td1.Visible = false;
                GridView1.Columns[8].Visible = false;
                td2.Visible = false;
                GridView2.Columns[8].Visible = false;
                if (CurrentUser.UnitId == "0")  //供应商查看，不显示后台用户上传的附件
                {
                    GridView2.Visible = false;
                    ta1.Visible = false;
                }

            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="fileid"></param>
        private void DeleteFile(string fileid)
        {
            try
            {
                DataSet ds = manager.Getpath(int.Parse(fileid));
                string path = ds.Tables[0].Rows[0]["F_Detail"].ToString();
               DataSet dd = ppbo.GetList(" FJID='" + ds.Tables[0].Rows[0]["F_FILEID"].ToString() + "'");
               if (dd.Tables[0].Rows.Count == 0)
                   dd = ppbo.GetList(" qefjid='" + ds.Tables[0].Rows[0]["F_FILEID"].ToString() + "'");
                if (manager.DeleteFile(fileid))
                {
                   // File.Delete(path);
                    AlertRedirect("Deleted successful", "PPARAttachmentTab.aspx?RID=" + dd.Tables[0].Rows[0]["ID"].ToString());
                }

            }
            catch (Exception)
            {

                throw;
            }
        }

        protected override void SkinControl()
        {
        }

        #region  上传
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["RID"] != null)
            {
                FJID = Request.QueryString["RID"];
                string FileName = file.FileName;
                if (!string.IsNullOrEmpty(FileName))
                {
                    string extension = System.IO.Path.GetExtension(FileName);//获取文件类型后缀名
                    if ((extension.ToLower() == ".pdf"))
                    {
                        int fileSize = file.PostedFile.ContentLength;
                        if (fileSize == 0)
                        {
                            base.AlertMessage("The file does not exist！");
                        }
                        else
                        {
                            if (true)//限制大小为1M
                            {
                                string time = DateTime.Now.ToString("yyyyMMddHHmmss");
                                string FileNam_Time = "";
                                int FileIndex = FileName.LastIndexOf("\\");
                                string FName = FileName.Substring(FileIndex + 1);
                                FileNam_Time = FName;//日期和名称组合
                                string path = HttpContext.Current.Request.MapPath("../FileUpload/QYRD/" + base.CurrentUser.Name + "/");
                                string url = "../FileUpload/QYRD/" + CurrentUser.Name + "/"+time+ FileNam_Time;
                                DirectoryInfo upDir = new DirectoryInfo(path);
                                if (!upDir.Exists)
                                {
                                    upDir.Create();
                                }
                                file.SaveAs(path + FileNam_Time);
                                ///加水印
                                string source = path + FileNam_Time; //模板路径
                                string output = path + time + FileNam_Time; //导出水印背景后的PDF
                                string watermark = "../FileUpload/ShuiYin/" + CurrentUser.Id + ".png";   // 水印图片
                                DirectoryInfo upDir2 = new DirectoryInfo(HttpContext.Current.Request.MapPath("../FileUpload/ShuiYin/"));
                                if (!upDir2.Exists)
                                {
                                    upDir2.Create();
                                }
                                if (File.Exists(watermark)) //如果存在，则删除重新生成
                                {
                                    File.Delete(watermark);
                                }
                                AddShuiYin(base.CurrentUser.Name + "(" + base.CurrentUser.Id + ")" + DateTime.Now.ToString("yyyyMMdd HHmmss"), watermark);
                                bool isSurrcess = PDFWatermark(source, output, MapPath(watermark), 10, 10);
                                if (isSurrcess)
                                    File.Delete(source); //删除原文件
                                //加水印
                                DataInfo(FJID, FileNam_Time, path + time, int.Parse(ddlType.SelectedValue), url, (fileSize/1024.0).ToString("n")+"kb","",CurrentUser.Id);
                                ShowData(FJID);
                            }
                        }
                    }
                    else
                    {
                        AlertMessage("Can't upload this file types");
                    }
                }
                else
                {
                    AlertMessage("Please select accessories！");
                }
            }
        }
        #endregion

        #region 添加水印
        /// <summary>
        /// PDF添加水印
        /// </summary>
        /// <param name="inputfilepath"></param>
        /// <param name="outputfilepath"></param>
        /// <param name="ModelPicName"></param>
        /// <param name="top"></param>
        /// <param name="left"></param>
        /// <returns></returns>
        public bool PDFWatermark(string inputfilepath, string outputfilepath, string ModelPicName, float top, float left)
        {
            PdfReader pdfReader = null;
            PdfStamper pdfStamper = null;
            try
            {
                pdfReader = new PdfReader(inputfilepath);

                int numberOfPages = pdfReader.NumberOfPages;

                iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(1);

                float width = psize.Width;

                float height = psize.Height;

                pdfStamper = new PdfStamper(pdfReader, new FileStream(outputfilepath, FileMode.Create));

                PdfContentByte waterMarkContent;

                iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(ModelPicName);

                // image.Width = 200;
                // image.Height = 50;

                image.GrayFill = 20;//透明度，灰色填充
                left = width - image.Width + left;
                //top = height - image.Height - top;
                image.SetAbsolutePosition(left, top);

                //每一页加水印,也可以设置某一页加水印 
                for (int i = 1; i <= numberOfPages; i++)
                {
                    waterMarkContent = pdfStamper.GetUnderContent(i);

                    waterMarkContent.AddImage(image);
                }
                return true;
            }
            catch (Exception ex)
            {
                AlertMessage(ex.Message.Trim());
                return false;
            }
            finally
            {

                if (pdfStamper != null)
                    pdfStamper.Close();

                if (pdfReader != null)
                    pdfReader.Close();
            }
        }
        #endregion

        //添加操作
        private void DataInfo(string F_FileID, string F_Filename, string path, int type, string url,string filesize,string remarks,string upby)
        {
            manager.InsertFile(F_FileID, F_Filename, path, type, url,filesize,remarks,upby,"","","");
        }

        #region 绑定已上传附件列表
        private void ShowData(string FileID)
        {
            DataSet ds = manager.GetFileList(FileID);
            if (ds.Tables[0].Rows.Count == 0)
            {
                DataRow dr = ds.Tables[0].NewRow();
                dr["attachmenttype"] = "<font color='red'>Not upload files</font>";
                dr["sx"] = "31";
                ds.Tables[0].Rows.Add(dr);
            }
            GridView1.DataSource = ds;
            GridView1.DataBind();
            LoadImg(ds);
        }

        private void ShowDataQE(string FileID)
        {
            DataSet ds = manager.GetFileList(FileID);
            if (ds.Tables[0].Rows.Count == 0)
            {
                DataRow dr = ds.Tables[0].NewRow();
                dr["attachmenttype"] = "<font color='red'>Not upload files</font>";
                dr["sx"] = "31";
                ds.Tables[0].Rows.Add(dr);
            }
            GridView2.DataSource = ds;
            GridView2.DataBind();
            LoadImg(ds);
        }
        #endregion

        #region 生成水印图片
        /// <summary>
        /// 生成水印图片
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="Path"></param>
        private void AddShuiYin(string Name, string Path)
        {
            //新建一个bmp图片
            System.Drawing.Image bitmap = new System.Drawing.Bitmap(260, 20);
            //新建一个画板
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
            //设置高质量插值法
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            //设置高质量,低速度呈现平滑程度
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //清空画布并以透明背景色填充
            g.Clear(System.Drawing.Color.Transparent);
            //在指定位置并且按指定大小绘制原图片的指定部分
            g.DrawImage(bitmap, new System.Drawing.Rectangle(0, 0, 260, 20),

            new System.Drawing.Rectangle(0, 0, 0, 0),

            System.Drawing.GraphicsUnit.Pixel);
            System.Drawing.Font f = new System.Drawing.Font("宋体", 12);
            System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
            g.DrawString(Name, f, b, 2, 2);
            try
            {
                //以jpg格式保存缩略图
                bitmap.Save(MapPath(Path), System.Drawing.Imaging.ImageFormat.Png);
            }
            catch (System.Exception ex)
            {
                AlertMessage(ex.Message.ToString());
            }
            finally
            {
                bitmap.Dispose();
                //bitmap.Dispose();
                g.Dispose();
            }
        }
        #endregion

        #region Image
        /// <summary>
        /// 加载附件类型
        /// </summary>
        /// <param name="ds"></param>
        private void LoadType(DataSet ds)
        {
            string TypeName = "";
            int ID = 0;
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                TypeName = dr["ATTACHMENTTYPE"].ToString();
                ID = int.Parse(dr["rownum"].ToString());
                switch (ID)
                {
                    case 1:
                        lblType1.Text = TypeName;
                        break;
                    case 2:
                        lblType2.Text = TypeName;
                        break;
                    case 3:
                        lblType3.Text = TypeName;
                        break;
                    case 4:
                        lblType4.Text = TypeName;
                        break;
                    case 5:
                        lblType5.Text = TypeName;
                        break;
                    case 6:
                        lblType6.Text = TypeName;
                        break;
                    case 7:
                        lblType7.Text = TypeName;
                        break;
                    case 8:
                        lblType8.Text = TypeName;
                        break;
                    case 9:
                        lblType9.Text = TypeName;
                        break;
                    case 10:
                        lblType10.Text = TypeName;
                        break;
                    case 11:
                        lblType11.Text = TypeName;
                        break;
                    case 12:
                        lblType12.Text = TypeName;
                        break;
                    case 13:
                        lblType13.Text = TypeName;
                        break;
                    case 14:
                        lblType14.Text = TypeName;
                        break;
                    case 15:
                        lblType15.Text = TypeName;
                        break;
                    case 16:
                        lblType16.Text = TypeName;
                        break;
                    case 17:
                        lblType17.Text = TypeName;
                        break;
                    case 18:
                        lblType18.Text = TypeName;
                        break;
                    case 19:
                        lblType19.Text = TypeName;
                        break;
                    case 20:
                        lblType20.Text = TypeName;
                        break;
                    case 21:
                        lblType21.Text = TypeName;
                        break;
                    case 22:
                        lblType22.Text = TypeName;
                        break;
                    case 23:
                        lblType23.Text = TypeName;
                        break;
                    case 24:
                        lblType24.Text = TypeName;
                        break;
                    case 25:
                        lblType25.Text = TypeName;
                        break;
                    case 26:
                        lblType26.Text = TypeName;
                        break;
                    case 27:
                        lblType27.Text = TypeName;
                        break;
                    case 28:
                        lblType28.Text = TypeName;
                        break;
                    case 29:
                        lblType29.Text = TypeName;
                        break;
                    case 30:
                        lblType30.Text = TypeName;
                        break;
                    default:
                        break;
                }
            }
        }

        /// <summary>
        /// 加载不需要上传的附件类型
        /// </summary>
        /// <param name="ds"></param>
        private void LoadFileSet(DataSet ds)
        {
            int ID = 0;
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                string url = "~/Part/image/3.png";
                ID = int.Parse(dr["sx"].ToString());
                switch (ID)
                {
                    case 1:
                        img1.ImageUrl = url;
                        break;
                    case 2:
                        img2.ImageUrl = url;
                        break;
                    case 3:
                        img3.ImageUrl = url;
                        break;
                    case 4:
                        img4.ImageUrl = url;
                        break;
                    case 5:
                        img5.ImageUrl = url;
                        break;
                    case 6:
                        img6.ImageUrl = url;
                        break;
                    case 7:
                        img7.ImageUrl = url;
                        break;
                    case 8:
                        img8.ImageUrl = url;
                        break;
                    case 9:
                        img9.ImageUrl = url;
                        break;
                    case 10:
                        img10.ImageUrl = url;
                        break;
                    case 11:
                        img11.ImageUrl = url;
                        break;
                    case 12:
                        img12.ImageUrl = url;
                        break;
                    case 13:
                        img13.ImageUrl = url;
                        break;
                    case 14:
                        img14.ImageUrl = url;
                        break;
                    case 15:
                        img15.ImageUrl = url;
                        break;
                    case 16:
                        img16.ImageUrl = url;
                        break;
                    case 17:
                        img17.ImageUrl = url;
                        break;
                    case 18:
                        img18.ImageUrl = url;
                        break;
                    case 19:
                        img19.ImageUrl = url;
                        break;
                    case 20:
                        img20.ImageUrl = url;
                        break;
                    case 21:
                        img21.ImageUrl = url;
                        break;
                    case 22:
                        img22.ImageUrl = url;
                        break;
                    case 23:
                        img23.ImageUrl = url;
                        break;
                    case 24:
                        img24.ImageUrl = url;
                        break;
                    case 25:
                        img25.ImageUrl = url;
                        break;
                    case 26:
                        img26.ImageUrl = url;
                        break;
                    case 27:
                        img27.ImageUrl = url;
                        break;
                    case 28:
                        img28.ImageUrl = url;
                        break;
                    //case 29:
                    //    img29.ImageUrl = url;
                    //    break;
                    //case 30:
                    //    img30.ImageUrl = url;
                    //    break;
                    default:
                        break;
                }
            }
        }

        /// <summary>
        /// 更新已上传图片
        /// </summary>
        /// <param name="ds"></param>
        private void LoadImg(DataSet ds)
        {
            string url = "~/Part/image/2.png";
            int ID = 0;
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                ID = int.Parse(dr["sx"].ToString());
                switch (ID)
                {
                    case 1:
                        img1.ImageUrl=url;
                        break;
                    case 2:
                        img2.ImageUrl = url;
                        break;
                    case 3:
                        img3.ImageUrl = url;
                        break;
                    case 4:
                        img4.ImageUrl = url;
                        break;
                    case 5:
                        img5.ImageUrl = url;
                        break;
                    case 6:
                        img6.ImageUrl = url;
                        break;
                    case 7:
                        img7.ImageUrl = url;
                        break;
                    case 8:
                        img8.ImageUrl = url;
                        break;
                    case 9:
                        img9.ImageUrl = url;
                        break;
                    case 10:
                        img10.ImageUrl = url;
                        break;
                    case 11:
                        img11.ImageUrl = url;
                        break;
                    case 12:
                        img12.ImageUrl = url;
                        break;
                    case 13:
                        img13.ImageUrl = url;
                        break;
                    case 14:
                        img14.ImageUrl = url;
                        break;
                    case 15:
                        img15.ImageUrl = url;
                        break;
                    case 16:
                        img16.ImageUrl = url;
                        break;
                    case 17:
                        img17.ImageUrl = url;
                        break;
                    case 18:
                        img18.ImageUrl = url;
                        break;
                    case 19:
                        img19.ImageUrl = url;
                        break;
                    case 20:
                        img20.ImageUrl = url;
                        break;
                    case 21:
                        img21.ImageUrl = url;
                        break;
                    case 22:
                        img22.ImageUrl = url;
                        break;
                    case 23:
                        img23.ImageUrl = url;
                        break;
                    case 24:
                        img24.ImageUrl = url;
                        break;
                    case 25:
                        img25.ImageUrl = url;
                        break;
                    case 26:
                        img26.ImageUrl = url;
                        break;
                    case 27:
                        img27.ImageUrl = url;
                        break;
                    case 28:
                        img28.ImageUrl = url;
                        break;
                    //case 29:
                    //    img29.ImageUrl = url;
                    //    break;
                    //case 30:
                    //    img30.ImageUrl = url;
                    //    break;
                    default:
                        break;
                }

            }
            DropDownList1_SelectedIndexChanged(null, null);
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataSet ds = typebo.GetIsLevel(DropDownList1.SelectedValue);
            int ID = 0;
            string url = "~/Part/image/2.png";
            string url2 = "~/Part/image/3.png";
            img1.Visible = img2.Visible = img3.Visible = img4.Visible = img5.Visible = img6.Visible = img7.Visible = img8.Visible = img9.Visible
                = img10.Visible = img11.Visible = img12.Visible = img13.Visible = img14.Visible = img15.Visible = img16.Visible = img17.Visible = img18.Visible
                = img10.Visible = img20.Visible = img21.Visible = img22.Visible = img23.Visible = img24.Visible = img25.Visible = img26.Visible = img17.Visible = img28.Visible = true;
            DataSet ids = ibo.GetList(" pparid=" + Request.QueryString["RID"].ToString());
            LoadFileSet(ids);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                ID = int.Parse(dr["sx"].ToString());
                switch (ID)
                {
                    case 1:
                        if(img1.ImageUrl!=url&&img1.ImageUrl!=url2)
                        img1.Visible=false;
                        break;
                    case 2:
                        if (img2.ImageUrl != url && img2.ImageUrl != url2)
                        img2.Visible = false;
                        break;
                    case 3:
                        if (img3.ImageUrl != url && img3.ImageUrl != url2)
                        img3.Visible = false;
                        break;
                    case 4:
                        if (img4.ImageUrl != url && img4.ImageUrl != url2)
                        img4.Visible = false;
                        break;
                    case 5:
                        if (img5.ImageUrl != url && img5.ImageUrl != url2)
                        img5.Visible = false;
                        break;
                    case 6:
                        if (img6.ImageUrl != url && img6.ImageUrl != url2)
                        img6.Visible = false;
                        break;
                    case 7:
                        if (img7.ImageUrl != url && img7.ImageUrl != url2)
                        img7.Visible = false;
                        break;
                    case 8:
                        if (img8.ImageUrl != url && img8.ImageUrl != url2)
                        img8.Visible = false;
                        break;
                    case 9:
                        if (img9.ImageUrl != url && img9.ImageUrl != url2)
                        img9.Visible = false;
                        break;
                    case 10:
                        if (img10.ImageUrl != url && img10.ImageUrl != url2)
                        img10.Visible = false;
                        break;
                    case 11:
                        if (img11.ImageUrl != url && img11.ImageUrl != url2)
                        img11.Visible = false;
                        break;
                    case 12:
                        if (img12.ImageUrl != url && img12.ImageUrl != url2)
                        img12.Visible = false;
                        break;
                    case 13:
                        if (img13.ImageUrl != url && img13.ImageUrl != url2)
                        img13.Visible = false;
                        break;
                    case 14:
                        if (img14.ImageUrl != url && img14.ImageUrl != url2)
                        img14.Visible = false;
                        break;
                    case 15:
                        if (img15.ImageUrl != url && img15.ImageUrl != url2)
                        img15.Visible = false;
                        break;
                    case 16:
                        if (img16.ImageUrl != url && img16.ImageUrl != url2)
                        img16.Visible = false;
                        break;
                    case 17:
                        if (img17.ImageUrl != url && img17.ImageUrl != url2)
                        img17.Visible = false;
                        break;
                    case 18:
                        if (img18.ImageUrl != url && img18.ImageUrl != url2)
                        img18.Visible = false;
                        break;
                    case 19:
                        if (img19.ImageUrl != url && img19.ImageUrl != url2)
                        img19.Visible = false;
                        break;
                    case 20:
                        if (img20.ImageUrl != url && img20.ImageUrl != url2)
                        img20.Visible = false;
                        break;
                    case 21:
                        if (img21.ImageUrl != url && img21.ImageUrl != url2)
                        img21.Visible = false;
                        break;
                    case 22:
                        if (img22.ImageUrl != url && img22.ImageUrl != url2)
                        img22.Visible = false;
                        break;
                    case 23:
                        if (img23.ImageUrl != url && img23.ImageUrl != url2)
                        img23.Visible = false;
                        break;
                    case 24:
                        if (img24.ImageUrl != url && img24.ImageUrl != url2)
                        img24.Visible = false;
                        break;
                    case 25:
                        if (img25.ImageUrl != url && img25.ImageUrl != url2)
                        img25.Visible = false;
                        break;
                    case 26:
                        if (img26.ImageUrl != url && img26.ImageUrl != url2)
                        img26.Visible = false;
                        break;
                    case 27:
                        if (img27.ImageUrl != url && img27.ImageUrl != url2)
                        img27.Visible = false;
                        break;
                    case 28:
                        if (img28.ImageUrl != url && img28.ImageUrl != url2)
                        img28.Visible = false;
                        break;
                    //case 29:
                    //    if (img29.ImageUrl != url && img29.ImageUrl != url2)
                    //    img29.Visible = false;
                    //    break;
                    //case 30:
                    //    if (img30.ImageUrl != url && img30.ImageUrl != url2)
                    //    img30.Visible = false;
                    //    break;
                    default:
                        break;
                }

            }

        }

        #endregion

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            lblErrMessage.Text = "";
            PPAR p = ppbo.GetModel(int.Parse(Request.QueryString["RID"] == null ? "0" : Request.QueryString["RID"].ToString()));
            if (p != null)
            {
                if (p.STATUS == 0)
                    Page.RegisterStartupScript("AlertRedirect", "<script language='javascript'>alert(\"Please first save PPAP\");</script>");
                //Page.RegisterStartupScript("AlertMessage", "<script language='javascript'>alert('Please first save PPAP');return false;</script>");
                else
                {
                    DataSet ds = manager.GetFileList(p.FJID);
                    if (ds.Tables[0].Rows.Count == 0)
                    {
                        Page.RegisterStartupScript("AlertRedirect", "<script language='javascript'>alert(\"No upload attachment\");</script>");
                    }
                    else
                    {
                        MailTemplate mail = mbo.GetModel(2);
                        DataSet dsmail = mbo.GetEDM2(p.ID.ToString());
                        if (mail != null&&dsmail.Tables[0].Rows.Count>0)
                        {
                            string title = mail.TITLE.Replace("{Part No.}", dsmail.Tables[0].Rows[0]["bdntpartno"].ToString());
                            title = title.Replace("{Part name}", dsmail.Tables[0].Rows[0]["bdntpartname"].ToString());
                            try
                            {
                                base.SendEmailToUser(dsmail.Tables[0].Rows[0]["email"].ToString(), title, mail.CONTENT,null);
                                lbo.WriteLog(CurrentUser.Id, "Send Email:" + dsmail.Tables[0].Rows[0]["email"].ToString(), Request.QueryString["rid"].ToString());
                                
                            }
                            catch (Exception ex)
                            {
                                AlertMessage(ex.Message.ToString());
                                lblErrMessage.Text = ex.Message.ToString();
                                lbo.WriteLog(CurrentUser.Id, "Send Email:" +ex.Message.ToString(), Request.QueryString["rid"].ToString());
                            }
                            p.STATUS = 2;
                            p.Submittedby = CurrentUser.Id;
                            p.SUBMISSIONLEVEL = int.Parse(DropDownList1.SelectedValue);
                            p.ISSPECIFICATIONS = rbtYes.Checked ? 1 : 0;
                            int num = new PPARBO().UpdateForSupplier(p);
                            lbo.WriteLog(CurrentUser.Id, "Submit PPAR", Request.QueryString["RID"].ToString());
                            if (num > 0)
                            {
                                Response.Write("<script language=javascript>top.frames['menu'].location.reload();</script>");
                                Response.Write("<script>top.frames['mainFrame'].location='PPAPTasks.aspx'</script>");
                            }
                        }
                       
                    }
                }
            }
        }

        /// <summary>
        /// 获取连接
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public string GetLink(string filename)
        {
            //string filepath = "ftp://" + ConfigurationManager.AppSettings["ftpServerIP"] + "/";
            string filepath = ConfigurationManager.AppSettings["FilePath"];
            return filepath + filename;
            
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            LinkButton link=(LinkButton)sender;
            if (File.Exists(ConfigurationManager.AppSettings["FilePath"]+link.Text))
            {
                FileInfo file = new FileInfo(ConfigurationManager.AppSettings["FilePath"]+link.Text);
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); //解决中文乱码
                Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name)); //解决中文文件名乱码    
                Response.AddHeader("Content-length", file.Length.ToString());
                Response.ContentType = "appliction/octet-stream";
                Response.WriteFile(file.FullName);
                Response.End();
            }
        }

        
    }
}